package adapter;

import android.content.Context;
import android.media.Image;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.squareup.picasso.Picasso;
import com.tikeyc.mobileplayer.R;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import domain.NetAudioPageItem;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import pl.droidsonroids.gif.GifImageView;
import utils.TimeUtils;

/**
 * Created by public1 on 2017/2/6.
 */

public class AudioNetPagerAdapter extends BaseAdapter {

    private static final int TYPE_VIDEO = 0;
    private static final int TYPE_IMAGE = 1;
    private static final int TYPE_GIF = 2;
    private static final int TYPE_TEXT = 3;
    private static final int TYPE_AD = 4;

    Context context;
    List<NetAudioPageItem.ListBean> audioItems;


    class ViewHolder {
        //user_info
        ImageView iv_headpic;
        TextView tv_name;
        TextView tv_time_refresh;
        ImageView iv_right_more;
        //bottom
        ImageView iv_video_kind;
        TextView tv_video_kind_text;
        TextView tv_shenhe_ding_number;
        TextView tv_shenhe_cai_number;
        TextView tv_posts_number;
        LinearLayout ll_download;

        //中间公共部分 所有的
        TextView tv_content;

        //Video
        TextView tv_play_name;
        TextView tv_video_duration;
        ImageView iv_commant;
        TextView tv_commant_context;
        JCVideoPlayerStandard jcv_videoplayer;

        //image
        ImageView iv_image_icon;

        //text
//        TextView tv_context;

        //Gif
        GifImageView iv_image_gif;
//        TextView tv_context;

        //ad
        Button btn_install;
//        TextView tv_context;
    }

    public AudioNetPagerAdapter(Context context,List<NetAudioPageItem.ListBean> audioItems) {
        this.context = context;
        this.audioItems = audioItems;


    }

    /**得到视图类型总数
     * @return
     */
    @Override
    public int getViewTypeCount() {
        return 5;
//        return super.getViewTypeCount();
    }

    /**根据位置得到对应的类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {

        NetAudioPageItem.ListBean listBean = audioItems.get(position);
        String type = listBean.getType();//video,text,image,gif,ad
        int itemViewTye = -1;

        if (type.equals("video")) {
            itemViewTye = TYPE_VIDEO;
        } else if (type.equals("image")) {
            itemViewTye = TYPE_IMAGE;
        } else if (type.equals("gif")) {
            itemViewTye = TYPE_GIF;
        } else if (type.equals("text")) {
            itemViewTye = TYPE_TEXT;
        } else {
            itemViewTye = TYPE_AD;
        }

        return itemViewTye;
//        return super.getItemViewType(position);
    }

    @Override
    public int getCount() {
        return audioItems.size();
    }

    @Override
    public Object getItem(int i) {
        return audioItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        int itemViewType = getItemViewType(i);
        ViewHolder viewHolder;

        if (view == null) {
            viewHolder = new ViewHolder();
            switch (itemViewType){
                case TYPE_VIDEO:{
                    view = View.inflate(context, R.layout.all_video_item,null);
                    viewHolder.tv_play_name = (TextView) view.findViewById(R.id.tv_play_name);
                    viewHolder.tv_video_duration = (TextView) view.findViewById(R.id.tv_video_duration);
//                    viewHolder.iv_commant = (ImageView) view.findViewById(R.id.iv_commant);
                    viewHolder.tv_commant_context = (TextView) view.findViewById(R.id.tv_commant_context);
                    viewHolder.jcv_videoplayer = (JCVideoPlayerStandard) view.findViewById(R.id.jcv_videoplayer);

                }
                break;
                case TYPE_IMAGE:{
                    view = View.inflate(context, R.layout.all_image_item,null);
                    viewHolder.iv_image_icon = (ImageView) view.findViewById(R.id.iv_image_icon);
                }
                break;
                case TYPE_GIF:{
                    view = View.inflate(context, R.layout.all_fig_item,null);
                    viewHolder.iv_image_gif = (GifImageView) view.findViewById(R.id.iv_image_gif);
                }
                break;
                case TYPE_TEXT:{
                    view = View.inflate(context, R.layout.all_text_item,null);
                }
                break;
                case TYPE_AD:{
                    view = View.inflate(context, R.layout.all_ad_item,null);
                    viewHolder.btn_install = (Button) view.findViewById(R.id.btn_install);
                    viewHolder.iv_image_icon = (ImageView) view.findViewById(R.id.iv_image_icon);
                }
                break;
                default:
                    break;
            }

            switch (itemViewType) {
                case TYPE_VIDEO:
                case TYPE_IMAGE:
                case TYPE_GIF:
                case TYPE_TEXT:{
                    //加载除开广告部分的公共视图
                    //top
                    viewHolder.iv_headpic = (ImageView) view.findViewById(R.id.iv_headpic);
                    viewHolder.tv_name = (TextView) view.findViewById(R.id.tv_name);
                    viewHolder.tv_time_refresh = (TextView) view.findViewById(R.id.tv_time_refresh);
                    viewHolder.iv_right_more = (ImageView) view.findViewById(R.id.iv_right_more);
                    //bottom
                    viewHolder.iv_video_kind = (ImageView) view.findViewById(R.id.iv_video_kind);
                    viewHolder.tv_video_kind_text = (TextView) view.findViewById(R.id.tv_video_kind_text);
                    viewHolder.tv_shenhe_ding_number = (TextView) view.findViewById(R.id.tv_shenhe_ding_number);
                    viewHolder.tv_shenhe_cai_number = (TextView) view.findViewById(R.id.tv_shenhe_cai_number);
                    viewHolder.tv_posts_number = (TextView) view.findViewById(R.id.tv_posts_number);
//                    viewHolder.ll_download = (LinearLayout) view.findViewById(R.id.ll_download);
                }
                break;
                case TYPE_AD:{

                }
                break;
                default:
                    break;
            }

            //中间公共部分，所有的都有
            viewHolder.tv_content = (TextView) view.findViewById(R.id.tv_content);


            if (view != null) view.setTag(viewHolder);

        }else {

            viewHolder = (ViewHolder) view.getTag();
        }


        //绑定数据
        bindData(i, itemViewType, viewHolder);


        return view;
    }

    /**根据类别绑定数据
     * @param i
     * @param itemViewType
     * @param viewHolder
     */
    private void bindData(int i, int itemViewType, ViewHolder viewHolder) {
        NetAudioPageItem.ListBean audioItem = audioItems.get(i);

        NetAudioPageItem.ListBean.VideoBean videoBean = audioItem.getVideo();
        switch (itemViewType){
            case TYPE_VIDEO:{
                bindData(viewHolder,audioItem);
                //第一个参数是视频地址，第二个是显示封面地址，第三个是标题
//                viewHolder.jcv_videoplayer.setUp(videoBean.getVideo().get(0),videoBean.getThumbnail().get(0),null);
                //最新版本修改了

                viewHolder.jcv_videoplayer.setUp(videoBean.getVideo().get(0),JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL,"");
                Picasso.with(context)
                        .load(videoBean.getThumbnail().get(0))
                        .into(viewHolder.jcv_videoplayer.thumbImageView);
                viewHolder.tv_play_name.setText(videoBean.getPlaycount() + "次播放发");
                viewHolder.tv_video_duration.setText(videoBean.getDuration() + "");

                if (audioItem.getTop_comments() != null && audioItem.getTop_comments().size() > 0 && audioItem.getTop_comments().get(0).getContent() != null) {
                    viewHolder.tv_commant_context.setText(audioItem.getTop_comments().get(0).getContent());
                }


            }
            break;
            case TYPE_IMAGE:{
                bindData(viewHolder,audioItem);

//                viewHolder.iv_image_icon.setImageResource(R.drawable.bg_item);
                int height = audioItem.getImage().getHeight() <= DensityUtil.getScreenHeight()*0.75?audioItem.getImage().getHeight() : (int)(DensityUtil.getScreenHeight()*0.75);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtil.getScreenWidth(),height);
                viewHolder.iv_image_icon.setLayoutParams(params);
                if (audioItem.getImage() != null && audioItem.getImage().getBig() != null && audioItem.getImage().getBig().size() > 0) {
//                    x.image().bind(viewHolder.iv_image_icon,audioItem.getImage().getBig().get(0));
                    Glide.with(context).load(audioItem.getImage().getBig().get(0)).placeholder(null)
                            .error(null).diskCacheStrategy(DiskCacheStrategy.ALL).
                            into(viewHolder.iv_image_icon);
                }
            }
            break;
            case TYPE_GIF:{
                bindData(viewHolder,audioItem);
                Log.e("TAG","audioItem.getGif().getImages().get(0)" + audioItem.getGif().getImages().get(0));
                Glide.with(context).load(audioItem.getGif().getImages().get(0)).diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .into(viewHolder.iv_image_gif);
            }
            break;
            case TYPE_TEXT:{
                bindData(viewHolder,audioItem);
            }
            break;
            case TYPE_AD:{
//                bindData(viewHolder,audioItem);
            }
            break;
            default:
                break;
        }

        //设置文本
        viewHolder.tv_content.setText(audioItem.getText());

    }


    /**绑定公共数据
     * @param viewHolder
     * @param audioItem
     */
    private void bindData(ViewHolder viewHolder, NetAudioPageItem.ListBean audioItem) {

        if (audioItem.getU() != null && audioItem.getU().getHeader() != null && audioItem.getU().getHeader().get(0) != null) {
            x.image().bind(viewHolder.iv_headpic,audioItem.getU().getHeader().get(0), ImageOptions.DEFAULT);
        }

        if (audioItem.getU() != null && audioItem.getU().getName() != null) {
            viewHolder.tv_name.setText(audioItem.getU().getName() + "");
        }

        viewHolder.tv_time_refresh.setText(audioItem.getPasstime());

        //设置标签
        List<NetAudioPageItem.ListBean.TagsBean> tagsBean = audioItem.getTags();
        if (tagsBean != null && tagsBean.size() > 0) {
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < tagsBean.size(); i++ ) {
                buffer.append(tagsBean.get(i).getName() + " ");
            }
            viewHolder.tv_video_kind_text.setText(buffer.toString());
        }

        //设置点赞，踩，转发
        viewHolder.tv_shenhe_ding_number.setText(audioItem.getUp());
        viewHolder.tv_shenhe_cai_number.setText(audioItem.getDown() + "");
        viewHolder.tv_posts_number.setText(audioItem.getForward() + "");
    }
}
